package cn.com.dhc.danlu.activity.coupon;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.fragment.base.BaseV4Fragment;
import cn.com.dhc.danlu.delegate.callback.ICouponListView;
import cn.com.dhc.danlu.delegate.delegate.coupon.DealersCouponUsedDelegate;
import cn.com.dhc.danlu.util.CommonConst.CouponConst;
import cn.com.dhc.danlu.util.ToastUtil;
import cn.com.dhc.danlu.widget.DanluCouponRecyclerView;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;
import cn.com.dhc.danlu.widget.loadlayer.LoaddingLayout;
import cn.com.hd.mall.web.webservices.entity.freedom.request.base.BaseRequest;
import cn.com.hd.mall.web.webservices.entity.freedom.request.coupon.GetCouponListReq;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-09-27
 * Time: 15:05
 * Description: 经销商红包 & 我的优惠券
 */
public class DealersUsedFragment extends BaseV4Fragment implements ICouponListView {
    private LoaddingLayout llLoading;
    private DanluCouponRecyclerView xrvCoupon;
    private DealersCouponUsedAdapter adapter;
    private DealersCouponUsedDelegate mDelegate;

    private GetCouponListReq mGetCouponListReq = new GetCouponListReq(agent.getToken());

    private FilterType mFilterType;
    private View.OnClickListener defaultClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 根据情况 跳转领券中心或是领取经销商红包
            Bundle data = new Bundle();
            switch (mFilterType) {
                //我的优惠券
                case dealers_coupon:
                    // 跳转领券中心
                    data.putSerializable(CouponConst.DEALERS_INTENT_TYPE, FilterType.dealers_coupon);
                    break;

                //经销商红包
                case dealers_envelop:
                    // 跳转经销商红包领取
                    data.putSerializable(CouponConst.DEALERS_INTENT_TYPE, FilterType.dealers_envelop);
                    break;
            }
            DealersCouponObtainActivity.startSelf(getContext(), data);
        }
    };

    public static DealersUsedFragment newInstance(FilterType filterType, String couponStatus) {
        DealersUsedFragment fragment = new DealersUsedFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(CouponConst.DEALERS_INTENT_TYPE, filterType);
        bundle.putString(CouponConst.DEALERS_INTENT_STATUS, couponStatus);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dl_fragment_coupon_used, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        mFilterType = (FilterType) bundle.getSerializable(CouponConst.DEALERS_INTENT_TYPE);
        initView();
        initApi();
        wrapReq();
        callApiToLoad();
    }

    @Override
    public void onDestroy() {
        mDelegate.detachHostView();
        super.onDestroy();
    }

    private void initView() {
        Bundle bundle = getArguments();
        xrvCoupon = (DanluCouponRecyclerView) getView().findViewById(R.id.xrv_coupon);
        llLoading = (LoaddingLayout) getView();
        String lastAppend = "";

        switch (mFilterType) {
            //我的优惠券
            case dealers_coupon:
                lastAppend = "优惠券";
                llLoading.setEmptyPic(R.drawable.no_envelop);
                llLoading.changeEmptyRetryText("去领券中心");
                break;

            //经销商红包
            case dealers_envelop:
                lastAppend = "红包";
                llLoading.setEmptyPic(R.drawable.ic_empty_jxsenvelop);
                llLoading.changeEmptyRetryText("抢经销商红包");
                break;
        }

        String status = bundle.getString(CouponConst.DEALERS_INTENT_STATUS);
        if (status != null) {
            switch (status) {
                case CouponConst.DEALERS_NOT_USE:
                    status = "未使用";
                    llLoading.isShowEmptyRetryBtn(true);
                    break;

                case CouponConst.DEALERS_HAS_USED:
                    status = "已使用";
                    llLoading.isShowEmptyRetryBtn(false);
                    break;

                case CouponConst.DEALERS_OUT_DATE:
                    status = "已过期";
                    llLoading.isShowEmptyRetryBtn(false);
                    break;
            }
        }

        llLoading.changeEmptyViewTips("当前没有" + status + lastAppend);
    }

    private void initApi() {
        mDelegate = new DealersCouponUsedDelegate();
        mDelegate.attachHostView(this);
        xrvCoupon.setRefreshView(mDelegate);
        mDelegate.setRefreshViewAttr(xrvCoupon);
        adapter = new DealersCouponUsedAdapter(getActivity(), mFilterType, true);
        xrvCoupon.setAdapter(adapter);
    }

    private void wrapReq() {
        Bundle bundle = getArguments();
        switch (mFilterType) {
            //我的优惠券
            case dealers_coupon:
                mGetCouponListReq.setDealerCouponType(CouponConst.DEALERS_COUPON);
                break;

            //经销商红包
            case dealers_envelop:
                mGetCouponListReq.setDealerCouponType(CouponConst.DEALERS_ENVELOPE);
                break;
        }
        mGetCouponListReq.setCouponStatus(bundle.getString(CouponConst.DEALERS_INTENT_STATUS));
    }

    private void callApiToLoad() {
        mDelegate.fetchData(true);
    }

    @Override
    public BaseRequest createFetchReqParams() {
        return mGetCouponListReq;
    }

    @Override
    public void onSuccess(@NonNull List datas, boolean isRefresh) {
        llLoading.setRetryClickListener(defaultClick);
        if (isRefresh) {
            //下拉刷新回调
            adapter.refreshAllData(datas);
            xrvCoupon.setLoadingMoreEnabled(true);
            if (llLoading.isSuccessView()) {
                // autorefresh | pullrefresh
                if (datas.isEmpty()) {
                    llLoading.showEmptyView();
                }
                xrvCoupon.refreshComplete();
            } else {
                // click view mask | default
                if (datas.isEmpty()) {
                    llLoading.showEmptyView();
                } else {
                    llLoading.showContentView();
                }
            }
        } else {
            if (datas.isEmpty()) {
                ToastUtil.getInstance().show("没有更多数据");
                xrvCoupon.setNoMore(true);
            } else {
                adapter.appendData(datas);
            }
            xrvCoupon.loadMoreComplete();
        }
    }

    @Override
    public void onError(@NonNull String errorInfo, boolean isRefresh) {
        if (isRefresh) {
            xrvCoupon.refreshComplete();
            // clear all
            adapter.refreshAllData(new ArrayList());
            llLoading.showErrorView(errorInfo);
            llLoading.setRetryClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    llLoading.showLoaddingView();
                    callApiToLoad();
                }
            });
        } else {
            ToastUtil.getInstance().show(errorInfo);
            xrvCoupon.loadMoreComplete();
        }
    }
}